#!/bin/sh

# Slackware build script for ATLAS

# Copyright 2010-2016 Serban Udrea <s.udrea@gsi.de>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification,
# is permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

PRGNAM=atlas
VERSION=${VERSION:-3.10.3}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH="$( uname -m )" ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
  BITSize="32" # Specifically for ATLAS
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
  BITSize="32" # Specifically for ATLAS
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
  BITSize="64" # Specifically for ATLAS
fi

# If you don't want to use architectural defaults set the following to
# something like "no".
#
USE_ARCH_DEFAULTS=${USE_ARCH_DEFAULTS:-yes}

# If you decide to use arch defaults and have some custom ones you may
# set the following variable to point to the directory containing these.
#
# If you decide to not use arch defauts and wish to create some after a build
# with full search, set the following variable to point to the directory where
# the file containing them should be placed.
# IMPORTANT: In this case, the file copied to ARCH_DEF_DIR will not be part of
#            the ATLAS package, to avoid problems in case of an upgrade on the
#            same machine. The value of ARCH_DEF_DIR will be written for your
#            reference to the file named ARCH_DEF_DIR within the doc directory
#            of ATLAS.
#
ARCH_DEF_DIR=${ARCH_DEF_DIR:-""}

# If you are on another architecture than x86 and/or don't use gcc you need to
# set the following variable to "yes".
#
USE_DWALL=${USE_DWALL:-no}

# You may wish to set the level 2 cache size to the proper value. The default
# is to deduce it from /proc/cpuinfo
#
L2_CACHE_SIZE=${L2_CACHE_SIZE:-"auto"}

if [ "$L2_CACHE_SIZE" = "auto" ]; then
  L2_CACHE_SIZE="$(cat /proc/cpuinfo |grep "cache size"| head -n 1| cut -d ":" -s -f2| cut -d " " -s -f2)"
  L2_SIZE_UNIT="$(cat /proc/cpuinfo |grep "cache size"| head -n 1| cut -d " " -s -f4)"
  case "$L2_SIZE_UNIT" in
      "KB") L2_CACHE_SIZE=$(($L2_CACHE_SIZE * 1024))
            ;;
      "MB") L2_CACHE_SIZE=$(($L2_CACHE_SIZE * 1024 *  1024))
            ;;
      "GB") L2_CACHE_SIZE=$(($L2_CACHE_SIZE * 1024 *  1024 * 1024))
            ;;
  esac
fi

# Check the value of L2_CACHE_SIZE
#
case "$L2_CACHE_SIZE" in
    ''|'0'|*[!0-9]*) echo "ERROR: The value of L2_CACHE_SIZE is not a strictly positive integer!"
                     exit 1
                     ;;
esac

# Set the (maximum) number of threads. If this is 0 just the serial libs get
# built, even on an SMP machine. By default it's set to -1 for autodetection.
#
NUM_THREADS=${NUM_THREADS:-"-1"}
case "$NUM_THREADS" in
           '-1'|'0') echo -n # Do nothing
                     ;;
                '1') NUM_THREADS="0" # One processor => no threading
                     ;;
        ''|*[!0-9]*) echo "ERROR: NUM_THREADS has an improper value!"
                     exit 1
                     ;;
esac

if [ $NUM_THREADS -gt 1 ]; then
  # On SMP machines one may want to set the processors to be used (see
  # atlas_install.pdf, p. 13). By default the list of processor ID's is empty
  # which means that ATLAS may use whatever is available.
  # NOTE: This is incompatible with the autodetection of the number of threads.
  #       Therefore NUM_THREADS must be greater than 1.
  #
  USE_PROCESSORS=${USE_PROCESSORS:-""}
  if [ -z "$USE_PROCESSORS" ]; then
    MT_SWITCH="-t $NUM_THREADS"
  else
    MT_SWITCH="--force-tids=\"$NUM_THREADS $USE_PROCESSORS\""
  fi
else
  MT_SWITCH="-t $NUM_THREADS"
fi

# Decide upon building full LAPACK or not. Set LAPACK_SOURCE to the empty
# string, if you don't want a full LAPACK build.
#
LAPACK_SOURCE=${LAPACK_SOURCE:-"/usr/share/lapack-atlas/lapack.tgz"}
if [ -z "$LAPACK_SOURCE" ]; then
  echo
  echo "WARNING"
  echo "WARNING: No LAPACK source specified. Just the highly restricted LAPACK"
  echo "         offered by ATLAS will get compiled!"
  echo "WARNING"
  echo
  sleep 3
else
  tar -tf "$LAPACK_SOURCE" > /dev/null 2>&1 || \
  { echo "ERROR: Improper LAPACK source archive!" \
    && echo "       Please check $LAPACK_SOURCE" \
    && echo "       and set it properly! " \
    && exit 1; } # NOTE: Here we just test that we deal with a tar archive.
  LAPACK_SOURCE="--with-netlib-lapack-tarfile=$LAPACK_SOURCE"

  # Change the following to yes if you would like to run the tests for LAPACK.
  #
  TEST_LAPACK="${TEST_LAPACK:-no}"
  # Make Y or N out of yes, Yes, No, no, etc.
  #
  TEST_LAPACK=$(echo "$TEST_LAPACK"|cut -b 1|tr a-z A-Z)
fi

# Decide upon building shared libraries or not. By default we ask for shared
# libs too. If one doesn't want this, she has to just set SHARED_SWITCH to the
# empty string.
#
SHARED_SWITCH=${SHARED_SWITCH:-"--shared"}

# This is the timer to be used for LAPACK. If you stay with gfortran,
# presently the default compiler on Slackware, you can leave the value as is.
# Otherwise, please read LAPACK's make.inc.example for more informations.
#
LAPACK_TIMER="${LAPACK_TIMER:-INT_ETIME}"

# This is the system destination directory. When installing the
# package produced by this script, ATLAS's files will be written to
# $SYS_DESTDIR/include, $SYS_DESTDIR/include/atlas, $SYS_DESTDIR/lib
# or $SYS_DESTDIR/lib64 ond appropriate platforms, etc.
# Nevertheless, by default the documentation files go to
# /usr/doc/$PRGNAM-$VERSION. You may change this through the variable
# DEFAULT_DOCS, see below.
#
SYS_DESTDIR=${SYS_DESTDIR:-/usr}

# Check if SYS_DESTDIR is an absolute path. If not, exit with error.
# NOTE: The $ is used because echo adds a \n at the end of the string.
#
echo $SYS_DESTDIR | grep -vE '/\.\./|/\.\.$' | grep -qE '^/' || \
{ echo "ERROR: The system destination directory has no absolute path!" \
&& echo "       The value of SYS_DESTDIR is $SYS_DESTDIR" \
&& echo "       Please set it properly! " \
&& exit 1; }

# You may want to have the documentation files installed under
# $SYS_DESTDIR/doc/$PRGNAM-$VERSION not /usr/doc/$PRGNAM-$VERSION.
# To achieve this just set the following variable to something like
# "no".
#
DEFAULT_DOCS=${DEFAULT_DOCS:-yes}

# The build directory to be created within the source directory of
# ATLAS.
#
BLDdir="BuildDir"

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT

cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/${PRGNAM}${VERSION}.tar.bz2
mv ATLAS $PRGNAM-$VERSION
cd $PRGNAM-$VERSION
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# Set the proper value to USE_ARCH_DEFAULTS, and the proper value to the
# configure switch needed in case you want to use custom arch defaults.
#
ARCH_DIR_SWITCH=""
case "$USE_ARCH_DEFAULTS" in
  [yY]|[yY][eE]|[yY][eE][sS]) USE_ARCH_DEFAULTS="1"
                              [ -z "$ARCH_DEF_DIR" ] || \
                              ARCH_DIR_SWITCH="-Ss ADdir $ARCH_DEF_DIR"
                              ;;
                           *) USE_ARCH_DEFAULTS="0" ;;
esac

mkdir -p $BLDdir
cd $BLDdir

# Configure atlas.
#
case "$USE_DWALL" in
  [yY]|[yY][eE]|[yY][eE][sS])
                             # Here we assume that we aren't on a x86 machine
                             # and/or gcc isn't the compiler to be used.
                             #
                             ../configure   $SHARED_SWITCH \
                                          --prefix="$SYS_DESTDIR" \
                                            $LAPACK_SOURCE \
                                            $MT_SWITCH \
                                           -Si archdef "$USE_ARCH_DEFAULTS" \
                                            $ARCH_DIR_SWITCH \
                                           -b "$BITSize" -D c -DWALL
                             ;;
                           *)
                             # Here we assume that we are on a x86 machine
                             # (be it 32 or 64 bits) and gcc is the compiler
                             # to be used.
                             #
                             # Get the CPU frequency for good timing.
                             #
                             CPU_FREQ="$(cat /proc/cpuinfo |grep "cpu MHz"| head -n 1| cut -d ":" -s -f2| tr -d [:blank:])"
                             #
                             ../configure   $SHARED_SWITCH \
                                          --prefix="$SYS_DESTDIR" \
                                            $LAPACK_SOURCE \
                                            $MT_SWITCH \
                                           -Si archdef "$USE_ARCH_DEFAULTS" \
                                            $ARCH_DIR_SWITCH \
                                           -b "$BITSize" \
                                           -D c -DPentiumCPS="$CPU_FREQ"
                             ;;
esac

# NOTES ON SOME FLAGS FOR CONFIGURE
#
#  SHARED_SWITCH = "--shared" asks for building the shared libraries too
# -Si archdef "$USE_ARCH_DEFAULTS" means that we ignore or not architectural defaults depending
#   upon the value of "$USE_ARCH_DEFAULTS".
# -b "$BITSize" tells ATLAS about the platform's bitsize, 32 or 64.
# -D c -DPentiumCPS="$CPU_FREQ" is for achieving good timing on x86 platforms with gcc.
# -D c -DWALL is for achieving good timing on non x86 platforms and/or non gcc compilers

# Write the value of L2_CACHE_SIZE to Make.inc
#
sed -i -r Make.inc -e \
    "s%L2SIZE = -DL2SIZE=[0-9]+%L2SIZE = -DL2SIZE=$L2_CACHE_SIZE%"

# Allow for deprecated LAPACK routines to get build in case of a full LAPACK
# installation. Also set the LAPACK timer to the desired value and add
# -frecursive to the compile flags, since this should help avoid problems
# with some functions which seem otherwise to not be thread safe.
#
if [ "$LAPACK_SOURCE" ]; then
  sed -i  ./src/lapack/reference/make.inc.example -e \
      "s%^#MAKEDEPRECATED *=.*Yes%MAKEDEPRECATED = Yes%"
  sed -i  ./interfaces/lapack/F77/src/Makefile -e \
      "s%NONE%$LAPACK_TIMER%" -e \
      "s%F77FLAGS)@%F77FLAGS) -frecursive@%" -e \
      "s%F77NOOPT)@%F77NOOPT) -frecursive@%"
fi

make build
make check

# If parallel libraries have been compiled check them too.
#
if [ -f lib/libptcblas.a ]; then
  make ptcheck
fi

# If the full LAPACK got build one may wish to test it too.
#
if [ "$LAPACK_SOURCE" ]; then
  if [ "$TEST_LAPACK" = "Y" ]; then
    ( cd src/lapack/reference
      [ -e ./libtmglib.a ] || make tmglib
      # Some testers segfault when build with -frecursive if one doesn't
      # increase the stack size limit, thus it's better to remove this flag
      # from make.inc
      #
      sed -i make.inc -e "s%-frecursive%%"

      # Now we have to set the proper library paths. Here for the serial libs.
      #
      ATLAS_LIBS="../../../../../lib/libf77blas.a ../../../../../lib/libcblas.a"
      ATLAS_LIBS="$ATLAS_LIBS ../../../../../lib/libatlas.a"
      LAPACK_LIB="../../../lib/liblapack.a"

      sed -i make.inc -e \
      "s%^BLASLIB *=.*%BLASLIB = $ATLAS_LIBS%" -e \
      "s%^CBLASLIB *=.*%CBLASLIB =%" -e \
      "s%^LAPACKLIB *=.*%LAPACKLIB = $LAPACK_LIB%"

      # Perform the tests.
      #
      make lapack_testing

      # Put the test results together
      #
      tar czf TEST_SERIAL_RESULTS.tgz TESTING/*.out

      # If threaded libs got build, we repeat the tests with them.
      #
      if [ -e ../../../lib/libptlapack.a ]; then
        make cleantesting
        ATLAS_LIBS="../../../../../lib/libptf77blas.a"
        ATLAS_LIBS="$ATLAS_LIBS ../../../../../lib/libptcblas.a"
        ATLAS_LIBS="$ATLAS_LIBS ../../../../../lib/libatlas.a -lpthread"
        LAPACK_LIB="../../../lib/libptlapack.a"
        sed -i make.inc -e \
        "s%^BLASLIB *=.*%BLASLIB = $ATLAS_LIBS%" -e \
        "s%^LAPACKLIB *=.*%LAPACKLIB = $LAPACK_LIB%"
        make lapack_testing
        tar czf TEST_PT_RESULTS.tgz TESTING/*.out
      fi
    )
  fi
fi

make install DESTDIR=${PKG}${SYS_DESTDIR}

# The install script (sometimes) "forgets" about libptlapack.a
#
cp -ua lib/libptlapack.a ${PKG}${SYS_DESTDIR}/lib/ || true

find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

# This is probably the easiest way to make sure that we install in the
# proper place.
#
if [ "$LIBDIRSUFFIX" ]; then
  mv ${PKG}${SYS_DESTDIR}/lib ${PKG}${SYS_DESTDIR}/lib${LIBDIRSUFFIX}
fi

# Create the doc directory for atlas and populate it.
#
case "$DEFAULT_DOCS" in
 [nN]|[nN][oO]) DOC_DIR="$PKG$SYS_DESTDIR/doc/$PRGNAM-$VERSION" ;;
             *) DOC_DIR="$PKG/usr/doc/$PRGNAM-$VERSION" ;;
esac
mkdir -p ${DOC_DIR}
cp -a ../INSTALL.txt ../README ../doc ${DOC_DIR}

# Add the Slackbuild script and README.SLACKWARE to the docs.
#
cat $CWD/$PRGNAM.SlackBuild > $DOC_DIR/$PRGNAM.SlackBuild
cat $CWD/README.SLACKWARE > $DOC_DIR/README.SLACKWARE

# Create custom arch defaults if appropriate.
#
if [ "$USE_ARCH_DEFAULTS" = "0"  ]; then
  if [ "$ARCH_DEF_DIR" ]; then
    ( cd ARCHS
    make ArchNew
    make tarfile
    cp -ua *.tar.* "$ARCH_DEF_DIR"
    )
    echo "$ARCH_DEF_DIR" > $DOC_DIR/ARCH_DEF_DIR
  fi
fi

# If the full LAPACK got installed add also some relevant files from its source
# tree.
#
if [ "$LAPACK_SOURCE" ]; then
  ( cd src/lapack/reference
    LAPACK_VER=$(./INSTALL/testversion | sed -e "s% *LAPACK *%%" -e "s% *%%g")
    LAPACK_DOC_DIR="${DOC_DIR}/lapack-$LAPACK_VER"
    mkdir "$LAPACK_DOC_DIR"
    cp -a LICENSE README "$LAPACK_DOC_DIR"

    # Copy the test results if present (getting around "set -e" with "echo -n").
    #
    cp -a TEST_* "$LAPACK_DOC_DIR" 2>/dev/null || echo -n
  )
fi

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd "$PKG"
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
